﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BTM_Client_Application.DeviceViewer
{
    /// <summary>
    /// Interaction logic for DeviceControlContainer.xaml
    /// </summary>
    public partial class DeviceControlContainer
    {
        //private Dictionary<string, ObservableCollection<DevicesTab>> Items { get; set; }
        //private ObservableCollection<TabItem> Tabs { get; set; }
        private ObservableCollection<DevicesTab> Tabs { get; set; }

        public DeviceControlContainer()
        {
            InitializeComponent();
            //TabsContainer.Items.SortDescriptions.Add(new SortDescription("Header", ListSortDirection.Ascending));
            TabsContainer.Items.SortDescriptions.Add(new SortDescription("DeviceTitle", ListSortDirection.Ascending));
        }

        public void AddDevice(string deviceTitle, string station, UserControl deviceControl)
        {
            DevicesTab t;
            try
            {
                t = Tabs.First(x => x.DeviceTitle == deviceTitle);
            }
            catch (Exception)
            {
                t = new DevicesTab(deviceTitle);
                Tabs.Add(t);
                var items = TabsContainer.Items;
                //int i = items.Cast<object>().TakeWhile(item => (item as DevicesTab).DeviceTitle != deviceTitle).Count();
                //TabsContainer.SelectedIndex = i;
            }
            try
            {
                var st = t.Devices.First(x => x.Station == station);
            }
            catch (Exception)
            {
                var container = new Container(station, deviceTitle, deviceControl);
                container.MiniWinClosed += ContainerOnMiniWinClosed;
                t.Devices.Add(new DeviceControlModel { Station = station, ControlContainer = container });
            }
            finally
            {
                Activate();
            }
        }

        private void ContainerOnMiniWinClosed(string station, string device)
        {
            RemoveDevice(device, station);
        }

        public void RemoveDevice(string deviceTitle, string station)
        {
            DevicesTab t;
            try
            {
                t = Tabs.First(x => x.DeviceTitle == deviceTitle);
            }
            catch (Exception)
            {
                return;
            }
            
            try
            {
                var st = t.Devices.First(x => x.Station == station);
                t.Devices.Remove(st);
                var disp = (st.ControlContainer as Container).DeviceControl as IDisposable;
                if (disp != null)
                {
                    disp.Dispose();     //Call disposing for control resources
                }
            }
            catch (Exception)
            {
                return;
            }
            if (t.Devices.Count < 1)
            {
                Tabs.Remove(t);
            }
        }

        private void DeviceControlContainer_OnInitialized(object sender, EventArgs e)
        {
            //Items = new Dictionary<string, ObservableCollection<DevicesTab>>();
            Tabs = new ObservableCollection<DevicesTab>();
            TabsContainer.ItemsSource = Tabs;
        }
    }
}
